package com.xbb.controller.user;

import com.xbb.common.RespResult;
import com.xbb.entity.Notice;
import com.xbb.mapper.NoticeMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    private String prefix = "/user/notice/";

    @Resource
    private NoticeMapper noticeMapper;

    // 公告管理
    @RequestMapping("/manage.html")
    public String manageHtml() {
        return prefix + "notice";
    }

    // 分页数据
    @ResponseBody
    @RequestMapping("/data")
    public RespResult data(@RequestParam("page") int page,
                           @RequestParam("limit") int limit,
                           @RequestParam(value = "title", required = false, defaultValue = "") String title) {
        if (title.equals("")) title = null;
        List<Notice> noticeList = noticeMapper.selectListByPaging((page - 1) * limit, limit, title);
        int count = noticeMapper.selectCountByPaging(title);
        RespResult respResult = new RespResult();
        respResult.success(noticeList, count);
        return respResult;
    }

    // 查看详情
    @RequestMapping("/item.html")
    public String itemHtml(@RequestParam("id") int id, Model model) {
        Notice notice = noticeMapper.selectById(id);
        model.addAttribute("notice", notice);
        return prefix + "item";
    }

}
